﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Configuration;

namespace ZG_member
{
    public partial class BalanceRemindForm : Form
    {
        MainForm mainform;
        public BalanceRemindForm(MainForm main)
        {
            
            InitializeComponent();
            mainform = main;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }

        

        private void MemberSaleForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer2.Start();
        }
        private void BalanceRemindForm_Load(object sender, EventArgs e)
        {
            
            this.Opacity = 0;
            this.timer1.Start();
            getBalanceList();
        }

        private void getBalanceList()
        {
            memberInfo memberfo = new memberInfo();

            DataTable dt = memberfo.GetList(" balance <=" + nudBalance.Value).Tables[0];

            dgvBalanceRmind.DataSource = dt;

            consumeSolution consume = new consumeSolution();
            Model.consumeSolution consumeMod = new Model.consumeSolution();
            Areas area = new Areas();
            ZG_member.Model.Areas areMode = new ZG_member.Model.Areas();
            taocanjiluBLL taocan = new taocanjiluBLL();
            Model.taocanjilu taocanMode = new Model.taocanjilu();
            string[] list = new string[] { };

            foreach (DataGridViewRow dgvr in dgvBalanceRmind.Rows)
            {
                // Shengyukecheng
                //  GetModelFormemberCardId()
                string cardType1 = dgvr.Cells["cardType1"].Value.ToString();
                if (cardType1 != "")
                {
                    int ID = int.Parse(dgvr.Cells["cardType1"].Value.ToString());
                    consumeMod = consume.GetModelList("id='" + ID + "'")[0];
                    dgvr.Cells["NewcardType"].Value = consumeMod.solutionName;
                }
                if (dgvr.Cells["AreasID"].Value != "")
                {
                    int id = int.Parse(dgvr.Cells["AreasID"].Value.ToString());
                    areMode = area.getModel(id);
                    dgvr.Cells["AreaName"].Value = areMode.AreaName;
                }
                //if (dgvr.Cells["Shengyukecheng"].Value != "")
                //{
                //    string memberId = dgvr.Cells["memberId"].Value.ToString();
                //    taocanMode = taocan.GetModelFormemberCardId(memberId);
                //    int shengyukecheng = 0;
                //    if (taocanMode == null)
                //    {
                //        dgvr.Cells["Shengyukecheng"].Value = 0;
                //        continue;
                //    }
                //    if (taocanMode.Xiangqing!=null)
                //    {
                //        list = taocanMode.Xiangqing.Split('|');
                //        for (int i = 0; i < list.Count()-1; i++)
                //        {
                //           shengyukecheng=shengyukecheng+ Convert.ToInt32(list[i].Substring(list[i].IndexOf(',') + 1));
                //        }
                //        dgvr.Cells["Shengyukecheng"].Value = shengyukecheng;
                //    }
                //    else
                //    {
                //        dgvr.Cells["Shengyukecheng"].Value = 0;
                //    }
                //}

            }
        }

        private void timer2_Tick_1(object sender, EventArgs e)
        {
            //timer关闭
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //timer开启
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }
        int flag = 0;
        private void BalanceRemindForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            getBalanceList();
        }
        public List<string> huoqueamil()
        {
            List<string> email = new List<string>();
            for (int i = 0; i < dgvBalanceRmind.Rows.Count; i++)
            {
                if (dgvBalanceRmind.Rows[i].Cells["Email"].Value.ToString() == null || dgvBalanceRmind.Rows[i].Cells["Email"].Value.ToString() == "")
                {
                    continue;
                }

                email.Add(dgvBalanceRmind.Rows[i].Cells["memberName"].Value.ToString() + "," + dgvBalanceRmind.Rows[i].Cells["Email"].Value.ToString());
            }
            return email;
        }
        public List<string> huoquphone()
        {
            List<string> email = new List<string>();
            for (int i = 0; i < dgvBalanceRmind.Rows.Count; i++)
            {
                if (dgvBalanceRmind.Rows[i].Cells["Tel"].Value.ToString() == null || dgvBalanceRmind.Rows[i].Cells["Tel"].Value.ToString() == "")
                {
                    continue;
                }

                email.Add(dgvBalanceRmind.Rows[i].Cells["Tel"].Value.ToString());
            }
            return email;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            

            List<string> email = huoqueamil();
            email ee = new email(email,0);
            ee.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            List<string> email = huoquphone();
            ZG_member.SendMsgsForm s = new ZG_member.SendMsgsForm(email);
            s.ShowDialog();
        }
        public void ExportExcel(string fileName, DataGridView myDGV)
        {
            string saveFileName = "";
            //bool fileSaved = false;
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "xls";
            saveDialog.Filter = "Excel文件|*.xls";
            saveDialog.FileName = fileName;
            saveDialog.ShowDialog();
            saveFileName = saveDialog.FileName;
            if (saveFileName.IndexOf(":") < 0) return; //被点了取消 
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
            {
                MessageBox.Show("无法创建Excel对象，可能您的机子未安装Office.Excel组件");
                return;
            }
            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 
            int colIndex = 0;
            //写入标题
            for (int i = 0; i < myDGV.ColumnCount; i++)
            {
                if (myDGV.Columns[i].Visible)
                {
                    colIndex++;
                    worksheet.Cells[1, colIndex] = myDGV.Columns[i].HeaderText;
                    //  worksheet.Cells[1, i] = myDGV.Columns[i].HeaderText;
                }
            }
            //写入数值
            for (int r = 0; r < myDGV.Rows.Count; r++)
            {
                colIndex = 0;
                for (int i = 0; i < myDGV.ColumnCount; i++)
                {
                    if (myDGV.Columns[i].Visible)
                    {
                        colIndex++;
                        worksheet.Cells[r + 2, colIndex] = myDGV.Rows[r].Cells[i].Value;
                    }
                }
                System.Windows.Forms.Application.DoEvents();
            }
            worksheet.Columns.EntireColumn.AutoFit();//列宽自适应

            if (saveFileName != "")
            {
                try
                {
                    workbook.Saved = true;
                    workbook.SaveCopyAs(saveFileName);
                    //fileSaved = true;
                }
                catch (Exception ex)
                {
                    //fileSaved = false;
                    MessageBox.Show("导出文件时出错,文件可能正被打开！/n" + ex.Message);
                }

            }
            xlApp.Quit();
            GC.Collect();//强行销毁 
            // if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //打开EXCEL

            MessageBox.Show(fileName + "导出成功", "系统提示", MessageBoxButtons.OK);
        }
        private void button4_Click(object sender, EventArgs e)
        {
            ExportExcel("", this.dgvBalanceRmind);
        }

        
    }
}
